﻿namespace com.crownpack
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;

    using ReferenceFieldValue = LocationLookupValue;

    using ReferenceObject = Customer;

    public class LocationLookupFieldControl : BaseFieldControl
    {
        #region Fields

        protected Label uiFull;
        protected DropDownList uiLocation;

        #endregion Fields

        #region Properties

        public override string DisplayTemplateName
        {
            get
            {
                return "LocationLookupDisplayRenderingTemplate";
            }
            set
            {
                base.DisplayTemplateName = value;
            }
        }

        public ReferenceFieldValue InternalObj
        {
            get;
            set;
        }

        public override object Value
        {
            get
            {
                EnsureChildControls( );
                if ( ControlMode == SPControlMode.Edit || ControlMode == SPControlMode.New )
                {
                    validate( );

                    string v = uiLocation.SelectedItem.Text.Trim( );

                    setOtherFieldValues( );

                    return v;
                }
                else
                    return base.Value;
            }
            set
            {
                EnsureChildControls( );
                if ( ItemFieldValue == null )
                    return;

                string v = ItemFieldValue.ToString( );

                if ( ControlMode == SPControlMode.Edit || ControlMode == SPControlMode.New )
                {
                    uiLocation.SelectedIndex = uiLocation.Items.IndexOf( uiLocation.Items.FindByText( v ) );
                }
                else
                {
                    InternalObj = new ReferenceFieldValue
                    {
                        Value = v
                    };

                    string s = InternalObj.GetHtmlFormat( );
                    if ( ControlMode == SPControlMode.Display )
                        uiFull.Text = s;
                }
                base.Value = value;
            }
        }

        protected override string DefaultTemplateName
        {
            get
            {
                if ( ControlMode == SPControlMode.Display )
                    return DisplayTemplateName;
                else
                    return "LocationLookupDefaultRenderingTemplate";
            }
        }

        bool IsRequired
        {
            get
            {
                return SPContext.Current.Fields.GetField( this.FieldName ).Required;
            }
        }

        #endregion Properties

        #region Methods

        protected override void CreateChildControls( )
        {
            base.CreateChildControls( );
            uiFull = ( Label ) TemplateContainer.FindControl( "uiFull" );
            uiLocation = ( DropDownList ) TemplateContainer.FindControl( "uiLocation" );

            labelRequired( );

            uiLocation_LoadValues( );
        }

        protected override void RenderFieldForDisplay( HtmlTextWriter output )
        {
            if ( ItemFieldValue == null )
            {
                output.Write( string.Empty );
                return;
            }
            string v = ItemFieldValue.ToString( );
            InternalObj = new ReferenceFieldValue( v );

            string s = InternalObj.GetHtmlFormat( );
            output.Write( s );
        }

        void labelRequired( )
        {
        }

        void setOtherFieldValues( )
        {
            ReferenceObject o = ReferenceObject.Deserialize( uiLocation.SelectedValue );
            SPListItem item = SPContext.Current.ListItem;
            SPField[ ] fields = SPContext.Current.ListItem.Fields.Cast<SPField>( ).ToArray( );
            for ( int i = 0; i < fields.Count( ); i++ )
            {
                if ( fields[ i ].InternalName.Equals( Layout.Address1, StringComparison.OrdinalIgnoreCase ) )
                    fields[ i ].ParseAndSetValue( item, o.Address1 );

                else if ( fields[ i ].InternalName.Equals( Layout.Branch, StringComparison.OrdinalIgnoreCase ) )
                    fields[ i ].ParseAndSetValue( item, o.Branch );

                else if ( fields[ i ].InternalName.Equals( Layout.BranchManager, StringComparison.OrdinalIgnoreCase ) )
                    fields[ i ].ParseAndSetValue( item, o.BranchManager );

                else if ( fields[ i ].InternalName.Equals( Layout.BranchName, StringComparison.OrdinalIgnoreCase ) )
                    fields[ i ].ParseAndSetValue( item, o.BranchName );

                else if ( fields[ i ].InternalName.Equals( Layout.GroupCode, StringComparison.OrdinalIgnoreCase ) )
                    fields[ i ].ParseAndSetValue( item, o.GroupCode );

                else if ( fields[ i ].InternalName.Equals( Layout.LocationNo, StringComparison.OrdinalIgnoreCase ) )
                    fields[ i ].ParseAndSetValue( item, o.LocationNo.HasValue ? o.LocationNo.Value.ToString( ) : 0.ToString( ) );

            }
        }

        void uiLocation_LoadValues( )
        {
            if ( uiLocation != null )
            {
                LOBDao dao = new LOBDao( );
                List<ReferenceObject> l = dao.GetByParentCustomerNo( 920010 );
                uiLocation.Items.Add( string.Empty );
                foreach ( ReferenceObject o in l )
                    uiLocation.Items.Add( new ListItem( o.LocationName, o.Serialize( ) ) );
            }
        }

        void validate( )
        {
            if ( IsRequired )
            {
                bool ok = true;

                // validate
                if ( uiLocation.SelectedIndex == 0 )
                    ok = false;

                if ( !ok )
                    //throw new SPFieldValidationException( "You must specify a value for all required fields." );
                    throw new SPFieldValidationException( "You must specify a value for this required field." );
            }
        }

        #endregion Methods
    }
}